/*
 * Created on Feb 20, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.query.project;

import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import com.hp.hpl.jena.rdf.model.Resource;

import edu.mit.simile.longwell.Cache;
import edu.mit.simile.longwell.Profile;
import edu.mit.simile.longwell.text.ITextIndexModel;

/**
 * @author dfhuynh
 */
public class TextIndexProjector extends ProjectorBase {
	final protected String	m_parameter;
	final protected String	m_locale;
	
	/**
	 * @param profile
	 */
	public TextIndexProjector(Profile profile, String parameter, String locale) {
		super(profile);
		m_parameter = parameter;
		m_locale = locale;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.project.ProjectorBase#internalProject()
	 */
	protected IProjection internalProject() {
		return new TextIndexProjection(m_locale, null);
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.project.ProjectorBase#internalProject(java.util.Set)
	 */
	protected IProjection internalProject(Set objects) {
		return new TextIndexProjection(m_locale, objects);
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.project.IProjector#isEfficientForRootProjection()
	 */
	public boolean isEfficientForRootProjection() {
		return true;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.project.IProjector#getUniqueness()
	 */
	public float getUniqueness() {
		return 1;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.project.IProjector#getParameter()
	 */
	public String getParameter() {
		return m_parameter;
	}

	/* (non-Javadoc)
	 * @see edu.mit.simile.longwell.query.project.IProjector#getLabel(java.lang.String)
	 */
	public String getLabel(String locale) {
		if (locale == null) {
			locale = m_locale;
		}
		
		ResourceBundle resources = ResourceBundle.getBundle(
			this.getClass().getName(),
			locale == null ? 
				Locale.getDefault() : new Locale(locale));

		return resources.getString("TextSearch");
	}
	
	protected ITextIndexModel getTextIndexModel() {
		return (ITextIndexModel) 
			m_profile.getStructuredModel(ITextIndexModel.class);
	}
	
	protected class TextIndexProjection 
		extends ProjectionBase implements ITextIndexProjection {

		final protected Set 	m_objects;
		final protected Cache	m_results = new Cache(10, TextIndexProjection.class, "results");
		
		protected TextIndexProjection(String locale, Set objects) {
			super(locale);
			m_objects = objects;
		}

		/* (non-Javadoc)
		 * @see edu.mit.simile.longwell.query.project.ProjectionBase#internalGetValue(com.hp.hpl.jena.rdf.model.Resource)
		 */
		protected Object internalGetValue(Resource object) {
			throw new InternalError("Not implemented");
		}

		/* (non-Javadoc)
		 * @see edu.mit.simile.longwell.query.project.ProjectionBase#internalGetObjects(java.lang.Object)
		 */
		protected Set internalGetObjects(Object value) {
			throw new InternalError("Not implemented");
		}

		/* (non-Javadoc)
		 * @see edu.mit.simile.longwell.query.project.ProjectionBase#getObjectToValueMap()
		 */
		protected Map internalGetObjectToValueMap() {
			throw new InternalError("Not implemented");
		}

		/* (non-Javadoc)
		 * @see edu.mit.simile.longwell.query.project.ITextIndexProjection#search(java.lang.String)
		 */
		public Set search(String text) {
			Set results = (Set) m_results.get(text);
			if (results == null) {
				results = internalSearch(text);
				m_results.put(text, results);
			}
			return results;
		}

		/* (non-Javadoc)
		 * @see edu.mit.simile.longwell.query.project.IProjection#getObjects()
		 */
		public Set getObjects() {
			return m_objects != null ? m_objects :
				m_profile.getSchemaModel().getAllObjects();
		}

		/* (non-Javadoc)
		 * @see edu.mit.simile.longwell.query.project.IProjection#getUniqueness()
		 */
		public float getUniqueness() {
			return 1;
		}
		
		protected Set internalSearch(String text) {
			ITextIndexModel tim = getTextIndexModel();
			
			Set objects = tim.search(text);
			if (m_objects != null) {
				Set objects2 = new HashSet(m_objects);
				
				objects2.retainAll(objects);
				
				objects = objects2;
			}
			
			return objects;
		}

		@Override
		public String getProperty() {
			// TODO Auto-generated method stub
			return null;
		}
	}
}
